<include file="header" />
<div class="wraper">
    <div class="load">
        <img class="load-bee" src="/assets/base/laundry/images/load-bee.png">

    </div>
	<div class="head clearfix">
		<a href="javascript:history.back();" class="iconfont icon-zuo pull-left"></a>
		<span class="head-name pull-left">{BEESCRM:$app_name}</span>
		<a href="{BEESCRM::U('/Base/laundry/index',array('laundryId'=>$laundryId))}" class="iconfont icon-shouye pull-left text-right"></a>
	</div>
	<div class="block-space text-center">
		<div class="pig"><img src="/assets/base/laundry/images/pig.png"/></div>
		智能洗衣采取预扣费模式，<br/>
		请先为你的钱袋子充点银两吧！
	</div>
	<div class="contain-box margin-top">
		<p class="amount">充值金额</p>
		<div class="tab-prize clearfix">
			<volist name="data['recharge']" id="vo">
			<if condition="$i lt 4">
			<span class="option sub-list <if condition='$i eq 1'>checked</if>" data-amount="{BEESCRM:$vo.value}">{BEESCRM:$vo.value}元</span>
			</if>
			</volist>
			<p class="option other clearfix">
				<select class="price-box" id="amount">
					<option value="0">其他</option>
					<volist name="data['recharge']" id="vo">
					<if condition="$i gt 3">
					<option value="{BEESCRM:$vo.value}">{BEESCRM:$vo.value}元</option>
					</if>
					</volist>
				</select>
				<span class="iconfont icon-zuo"></span>
			</p>
		</div>
		<p class="result-tips">您充值金额所对应的时间为<span id="hour" data-value="{BEESCRM:$data['recharge'][0]['value']*$data['ratio']}">{BEESCRM:$data['recharge'][0]['value']*$data['ratio']}分钟</span></p>
	</div>
	<div class="tariff">
		<span class="tariff-tag">温馨提示</span>
		<p style="color:red">充值标准为{BEESCRM:$data['min_recharge']}元/{BEESCRM:$data['min_charge_minute']}分钟</p>
	</div>
	<div class="btn-box">
		<a href="#" class="btn btn-submit">立即充值</a>
		<input type="hidden" id="ratio" value="{BEESCRM:$data.ratio}">
		<input type="hidden" id="ratio_num" value="{BEESCRM:$data.ratio_num}">
	</div>
	<div class="bottom-box">
		<p class="copyright">杭州青橄榄网络技术有限公司 技术支持</p>
	</div>
</div>
<include file="commonJS" />
<script>
	var height = $(document).height();
	var bottombox_height = $('.bottom-box').outerHeight();
	$('.wraper').css("minHeight",height);
	$('.wraper').css("paddingBottom",bottombox_height);

	$(window).load(function(){
		$('.load').hide();
	});
	$('.tab-prize').on('click','.option',function(){
		if($(this).hasClass('other')){
			$(this).addClass('checked').siblings().removeClass('checked');
		}
		else{
			$(this).toggleClass('checked').siblings().removeClass('checked');
		}
	});
</script>
<script>
	//去除左右空格
    $(function(){
    	var ratio_num=Number($('#ratio_num').val());
    	var ratio = $('#ratio').val();
		//var order_id=$('#order_id').val();
		var amountReg = /^[0-9]*[1-9][0-9]*$/;
		//确认付款
		$(document).on('click','.btn-submit',function(){
			var hour=Number($('#hour').data('value'));
			var amount=hour/ratio;
			if(amount==0){
				$.alert('error','充值金额不能为空');return ;
			}else if(!amountReg.test(amount)){
				$.alert('error','充值金额只能为正整数');return ;
			}
			$.ajax({
				type:'post',
				dataType:'json',
				data:{'amount':amount},
				url:"{BEESCRM::U('/base/laundry/createOrder',array('laundryId'=>$laundryId))}",
				beforeSend: function(){	 
					 $('.load').show();
					// Handle the beforeSend event	
				},
				success:function(data){
					console.log(data); $('.load').hide();
					if(data.status == 'F'){
						$.alert('error', data.message);return ;
					}else{
						/* alert(data.data.charge_log_id);
						return ; */
						/*  if("598" == "{BEESCRM::session('fans_id')}"){
							paySuccess(data.data.charge_log_id);
						}else{
							pay(data.wxpay);
						}  */
						var id = data.data.order_id;
						var wxpay = $.parseJSON(data.wxpay);
						pay(wxpay, id);
					}
				},
				error:function(){
					$('.load').hide();
					$.alert('error', data.message);
					return false;
				}
			});
		});
		
		//数据处理错误
		function show_error(data){
			$.alert('error',data.message);
			/* if(data.url){
				setTimeout('location.href="'+data.url+'"',2000);
			} */
		}
		
		//微支付
		 function pay(wxpay, id){
			if (typeof WeixinJSBridge == "undefined"){
				if( document.addEventListener ){
					document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
				}else if (document.attachEvent){
					document.attachEvent('WeixinJSBridgeReady', jsApiCall);
					document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
				}
			}else{
				WeixinJSBridge.invoke(
					'getBrandWCPayRequest',
					 wxpay,
					function(res){
						WeixinJSBridge.log(res.err_msg);
						if(res.err_msg == "get_brand_wcpay_request:ok"){
							paySuccess(id);
						}else if(res.err_msg == "get_brand_wcpay_request:cancel"){
							$.alert('error','付款已取消');
							return false;
						}else{
							alert(res.err_msg);
							$.alert('error','调用微支付失败');
							return false;
						}
					}
				);
			}
		}
		
		function paySuccess(id){
			var json = {};
			json.url = "{BEESCRM::U('base/laundry/paysuccess',array('laundryId' => $laundryId))}/order_id/"+id;
			$.getAjax(json,false,redirect);
		}
		
		function redirect(data){
			location.href = data.url;
		}
	});
	$(function(){
		var ratio_num=Number($('#ratio_num').val());
		var ratio = $('#ratio').val();
		$('.tab-prize').on('click','.option',function(){
			if($(this).hasClass('other')){
				$(this).siblings().removeClass('checked');
				$('#amount').change(function(){
					var num=Number($(this).val());
					var text = num*ratio;
					if(num!=0){
						$('#hour').text(text+'分钟');
						$('#hour').data('value',text);
					}
				})
					
			}
			else{
				if(!$(this).hasClass('checked')){
					$(this).toggleClass('checked').siblings().removeClass('checked');
				}
				$('#amount').val(0);
				var num=Number($(this).data('amount'));
				num=num*ratio;
				$('#hour').text(num+'分钟');
				$('#hour').data('value',num);
				
			}
		})
	});
</script>
</body>
</html>